//
//  DWJUserAccount.swift
//  weibo
//
//  Created by 代伟佳 on 16/4/21.
//  Copyright © 2016年 代伟佳. All rights reserved.
//

import UIKit

class DWJUserAccount: NSObject,NSCoding {

    var access_token: String?
    
    var expires_in: Int = 0 {
    
        didSet {
            
            // 当前秒数凭借一个秒数
            expires_date = NSDate().dateByAddingTimeInterval(Double(expires_in))
            
        }
    }
    
    var uid: String?
    
    // 用户头像
    var avatar_large: String?
    
    // 用户昵称
    var screen_name: String?
    
    // 便于我们判断token是否过期
    var expires_date: NSDate?
    
    init(dict: [String: AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
        
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    
    override var description: String {
    
        let keys = ["access_token","expires_in","uid","avatar_large","screen_name","expires_date"]
    
        return dictionaryWithValuesForKeys(keys).description
    }
    
    
    // 归档
    func encodeWithCoder(aCoder: NSCoder) {

        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
    }
    
    // 解档
    required init?(coder aDecoder: NSCoder) {

        
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate

        
        
    }
}
